<?php

class db
{
    //私有变量保存实例
    private static $_instance;

    //定义一个私有静态变量保存数据库连接
    private static $_conn;

    private function __construct() {}

    //定义获取数据库实例的方法
    public static function getInstance()
    {
        if (self::$_instance === null) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    public static function connect()
    {
        if (! self::$_conn) {
            self::$_conn = new mysqli('127.0.0.1', 'root', 'root', 'category');
        }

        mysqli_query(self::$_conn, "set names UTF8");

        return self::$_conn;
    }
}